package com.ecoolex.chinadutyfree.retailer.api.graphql;

import java.math.BigDecimal;
import java.util.Date;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import com.ecoolex.chinadutyfree.retailer.entity.Coupon;
import com.ecoolex.chinadutyfree.retailer.entity.CouponClass;
import com.ecoolex.chinadutyfree.retailer.ws.CouponService;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMemberAuthority;
import com.ecoolex.framework.modularize.api.service.currency.CurrencyType;
import com.ecoolex.framework.modularize.api.service.currency.CurrencyTypeService;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;

@GraphQL2Module(type = Type.User, description = "商户内部优惠券")
public class CouponGraphql {
	
	@Reference
	private CouponService couponService;
	
	@Autowired
	private CurrencyTypeService currencyTypeService;
	
	@TypeField(type = Coupon.class, field = "currencyType", description = "币种")
	public CurrencyType currencyType(@Source Coupon source) {
		return currencyTypeService.get(source.getCurrencyType());
	}
	
	@TypeField(type = Coupon.class, field = "couponClass", description = "优惠券类")
	public CouponClass couponClass(@Source Coupon source) {
		return couponService.getCouponClassById(source.getCouponClassId());
	}
	
	@TypeField(type = CouponClass.class,field = "canReceive",description = "能领取")
	public boolean canReceive(@Source CouponClass source,ConsumerMemberAuthority user) {
		return Check.isNull(couponService.getByUserIdAndCouponClassId(user.userId(), source.getCouponClassId()));
	}
	
	@TypeField(type = Coupon.class,field = "canUsed",description = "能使用")
	public Boolean canUsed(@Source Coupon source,@NonNull BigDecimal realPrice,ConsumerMemberAuthority user) {
		return couponService.checkCoupon(new Date(),realPrice, source.getCouponId());
	}
	
	@QueryField(field = "couponClassList",description = "商户优惠券列表")
	public Pagination<CouponClass> querycouponClass(@NonNull Page page,ConsumerMemberAuthority user) {
		user.assertLogin();
		return couponService.queryCouponClassForUser(page);
	}

	@MutationField(field = "receiveCoupon",description = "用户领取优惠券")
	public Coupon receiveCoupon(@NonNull Integer couponClassId,ConsumerMemberAuthority user) {
		user.assertLogin();
		return couponService.receiveCoupon(couponClassId, user.userId(), user.userName());
	}
	
	@QueryField(field = "userCouponList",description = "用户优惠券列表")
	public Pagination<Coupon> couponList(@NonNull Page page,ConsumerMemberAuthority user){
		user.assertLogin();
		return couponService.couponList(user.userId(), page);
	}
}
